//
//  CommonFunctions.m
//  Yemao
//
//  Created by team on 12-5-24.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CommonFunctions.h"

@implementation CommonFunctions

+ (BOOL)validateEmail:(NSString *)email
{
    NSString *regexString = @"\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    
    NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:regexString options:NSRegularExpressionCaseInsensitive error:nil];
    NSUInteger numberOfMatches = [reg numberOfMatchesInString:email options:0 range:NSMakeRange(0, [email length])];
    
    if (numberOfMatches == 0) 
    {
        DLog(@"email not match");
        return NO;
    }
    
    return YES;
}

 
+(NSString *)nianYueRiStringConvertDateString:(NSString *)strDate withFormatter:(NSString *)withFormatter
{
     NSString * str = [strDate stringByReplacingOccurrencesOfString:@"年" withString:withFormatter];
    str = [str stringByReplacingOccurrencesOfString:@"月" withString:withFormatter];
    str = [str stringByReplacingOccurrencesOfString:@"日" withString:withFormatter];
    str = [str substringToIndex:str.length-1];
    
    return str;
}


+ (NSString *)convertDate:(NSDate *)date withFormatter:(NSString *)withFormatter subFromIndex:(int)subIndex
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:withFormatter];
    NSString *newDateString = [formatter stringFromDate:date];
    
    NSString *tempStr = [newDateString substringFromIndex:subIndex];
    if (subIndex == 5 && [[tempStr substringToIndex:1] intValue] == 0) {
        tempStr = [tempStr substringFromIndex:1];
    }
    return tempStr;
}

+ (NSString *)intervalSinceNow:(int)intDate
{
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:intDate];
    
    NSInteger interval = [date timeIntervalSinceNow] * -1;
    
    NSString *timeString = @"";
    
    if (interval/3600 < 1) 
    {
        timeString = [NSString stringWithFormat:@"%d分钟前", (int)interval/60];
        if ([timeString isEqualToString:@"0分钟前"]) {
            timeString = @"刚刚";
        }
    }
    else if (interval/3600 >= 1 && interval/86400 < 1)
    {
        timeString = [NSString stringWithFormat:@"%d小时前", (int)interval/3600];
    }
    else if (interval/86400 >= 1 && interval/86400 < 10){
        timeString = [NSString stringWithFormat:@"%d天前", (int)interval/86400];
    }
    else if (interval >= 864000)
    {
        if (interval >= 86400*365) {
            timeString = [CommonFunctions convertDate:date withFormatter:@"yyyy年MM月dd日" subFromIndex:0];
        }
        else
            timeString = [CommonFunctions convertDate:date withFormatter:@"yyyy年MM月dd日" subFromIndex:5];
    }
    
    return timeString;
}

+ (NSString *)intervalSinceNowChating:(int)intDate
{
    NSDate *dateNow = [NSDate date];
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:intDate];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:dateNow];
    NSInteger yearNow = [comps year];
    NSInteger monthNow = [comps month];
    NSInteger dayNow = [comps day];
    
    comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
    NSInteger year = [comps year];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    
    int interval = [date timeIntervalSinceNow] * -1;
    NSString *timeString = @"";
    //今天
    if (year == yearNow && month == monthNow && day == dayNow) {
        timeString = [CommonFunctions convertDate:date withFormatter:@"HH:mm" subFromIndex:0];
        NSArray *aryHourMin = [timeString componentsSeparatedByString:@":"];
        int hour = [[aryHourMin objectAtIndex:0] intValue];
        if (hour < 12) {
            timeString = [NSString stringWithFormat:@"上午%d:%@",hour,[aryHourMin objectAtIndex:1]];
        }
        else
            timeString = [NSString stringWithFormat:@"下午%d:%@",((hour-12)==0?12:(hour-12)),[aryHourMin objectAtIndex:1]];
    }
    else
    {
        //去年
        if (interval >= 86400*365) {
            timeString = [CommonFunctions convertDate:date withFormatter:@"yyyy年MM月dd日 HH:mm" subFromIndex:0];
        }
        else
            timeString = [CommonFunctions convertDate:date withFormatter:@"yyyy年MM月dd日 HH:mm" subFromIndex:5];
    }
    return timeString;
}

+(NSString *)intervalSinceNowUserInfo:(int)intDate
{
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:intDate];
    NSInteger interval = [date timeIntervalSinceNow] * -1;
    
    NSString *timeString = @"";
    
    if (interval/3600 < 1)
    {
        timeString = [NSString stringWithFormat:@"%d分钟", (int)interval/60];
        if ([timeString isEqualToString:@"0分钟"]) {
            timeString = @"刚刚";
        }
    }
    else if (interval/3600 >= 1 && interval/86400 < 1)
    {
        timeString = [NSString stringWithFormat:@"%d小时", (int)interval/3600];
    }
    else if (interval/86400 >= 1 && interval/86400 < 10){
        timeString = [NSString stringWithFormat:@"%d天", (int)interval/86400];
    }
    else if (interval >= 864000)
    {
        if (interval >= 86400*365)
        {
            timeString = [CommonFunctions convertDate:date withFormatter:@"yyyy/MM/dd" subFromIndex:0];
        }
        else
            timeString = [CommonFunctions convertDate:date withFormatter:@"yyyy/MM/dd" subFromIndex:5];
    }
    return timeString;
}

+(NSString *)timeWithTodayOrYearMonthDayHoursMinutes:(int)intDate
{
    NSDate *dateNow = [NSDate date];
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:intDate];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:dateNow];
    NSInteger yearNow = [comps year];
    NSInteger monthNow = [comps month];
    NSInteger dayNow = [comps day];
    
    comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
    NSInteger year = [comps year];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    
    int interval = [date timeIntervalSinceNow] * -1;
    NSString *timeString = @"";
    //今天
    if (year == yearNow && month == monthNow && day == dayNow) {
        timeString = [CommonFunctions convertDate:date withFormatter:@"HH:mm" subFromIndex:0];
        timeString = [NSString stringWithFormat:@"今天 %@",timeString];
    }
    else
    {
        //去年
        if (interval >= 86400*365) {
            timeString = [CommonFunctions convertDate:date withFormatter:@"yyyy年MM月dd日 HH:mm" subFromIndex:0];
        }
        else
            timeString = [CommonFunctions convertDate:date withFormatter:@"yyyy年MM月dd日 HH:mm" subFromIndex:5];
    }
    return timeString;
}

+ (NSString *)timeWithYearMonthDay:(int)intDate
{
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:intDate];
    NSString *newDateString = [CommonFunctions convertDate:date withFormatter:@"yyyy-MM-dd" subFromIndex:0];
    return newDateString;
}

+ (NSString *)timeWithMonthDayHoursPoints:(int)intDate
{
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:intDate];
    NSString *newDateString = [CommonFunctions convertDate:date withFormatter:@"MM-dd HH:mm" subFromIndex:0];
    return newDateString;
}

+ (NSString *)timeWithYearMonthDayHoursMinutes:(int)intDate formatter:(NSString *)yearMonthDayHoursMinutes
{
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:intDate];
    NSString *newDateString = [CommonFunctions convertDate:date withFormatter:yearMonthDayHoursMinutes subFromIndex:0];
    return newDateString;
}



+ (NSUInteger)getWeekdayFromDate:(NSDate *)date
{
    NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents* components = [[NSDateComponents alloc] init];
    
    NSInteger unitFlags = NSYearCalendarUnit |NSMonthCalendarUnit |NSDayCalendarUnit |NSWeekdayCalendarUnit |NSHourCalendarUnit |
    NSMinuteCalendarUnit |NSSecondCalendarUnit;
    
    components = [calendar components:unitFlags fromDate:date];
    
    NSUInteger weekday = [components weekday];
    if (weekday == 1)
    {
        return 7;
    }
    return weekday-1;
    
}

+(NSString *)getDistanceWithFloat:(float)distanceFloat
{
    distanceFloat =  distanceFloat/1000;
    if (distanceFloat >= 1000.0f)
    {
        return [NSString stringWithFormat:@"%.1fkm",distanceFloat];
    }
    //    else if (distanceFloat >= 1.0f)
    //    {
    //        return [NSString stringWithFormat:@"%.1fkm",distanceFloat];
    //    }
    else
    {
        return [NSString stringWithFormat:@"%.2fkm",distanceFloat];
    }
}


+ (void)transformBtn:(UIButton *)theButton withAngle:(CGFloat)angle
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    
    theButton.transform = CGAffineTransformMakeRotation(angle);
    
    [UIView commitAnimations];
}

+ (void)transformtoOriginalBtn:(UIButton *)theButton
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    
    theButton.transform = CGAffineTransformIdentity;
    
    [UIView commitAnimations];
}

+ (UIImage *)scaleImage:(UIImage *)img size:(CGSize)size
{  
    // 创建一个bitmap的context  
    // 并把它设置成为当前正在使用的context  
    UIGraphicsBeginImageContext(size);  
    // 绘制改变大小的图片  
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];  
    // 从当前context中创建一个改变大小后的图片  
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
    // 使当前的context出堆栈  
    UIGraphicsEndImageContext();  
    // 返回新的改变大小后的图片  
    return scaledImage;  
}

+ (UIImage *)imageFromColor:(UIColor *)color imageSize:(CGSize)imageSize
{
    CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

@end



